<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    request.setAttribute("path", path);
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <title>入库审核记录</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"/>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.js"></script>

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>采购审核</legend>
</fieldset>
<%--table表 待审核表--%>
<table class="layui-hide" id="allApply" lay-filter="allApply"></table>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>审核记录</legend>
</fieldset>
<table class="layui-table" id="auditHistory" lay-filter="auditHistory"></table>

<%--头部栏--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container" >
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
    </div>
</script>
<%--右侧操作栏--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="agree">同意</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disagree">不同意</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allInfo">查看详情</a>
</script>

<%--审核记录表的右侧工具栏--%>
<script type="text/html" id="historyBarDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allAuditInfo">查看详情</a>
</script>

</body>
<script type="text/javascript" >
    layui.use(['table','form','jquery','layer'], function() {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        $(function () {
            //当页面加载完成后执行的事件
            allApply();
            auditHistory();
            detailInfo();

        });


        //生成待审核表
        function allApply(){
            table.render({
                elem: '#allApply'
                ,url:'${pageContext.request.contextPath}/request/showNoAudit'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,title: '书本库存'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID',fixed: 'left',hide:true,unresize: true, sort: true}
                    ,{field:'number', title:'申请采购编号'}
                    ,{field:'staffid', title:'申请人id',width:100}
                    ,{field:'staffname', title:'申请人姓名',width:100}
                    ,{field:'bookcount', title:'总量(本)',sort: true,width:120}
                    ,{field:'bookprice', title:'总价(元)',sort: true,width:80}
                    ,{field:'createdate', title:'申请时间'}
                    ,{field:'status', title:'审核状态',width:120,templet:function (d) {
                            if(d.status==0){
                                return "<button class='layui-btn layui-btn-xs layui-btn-normal'>待审核</button>";
                            }else if(d.status==1){
                                return "<button class='layui-btn layui-btn-xs layui-btn-danger'>审核通过</button>";
                            }else {
                                return "<button class='layui-btn layui-btn-xs layui-btn-danger'>被驳回</button>";
                            }
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
                ,text:{
                    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                }
            });
        }

        //审核记录表
        function auditHistory(){
            table.render({
                elem: '#auditHistory'
                ,url:'${pageContext.request.contextPath}/audit/AllAudit'
                ,title: '审核记录表'
                ,cols: [[
                    ,{field:'id', title:'ID',fixed: 'left',hide:true,unresize: true, sort: true}
                    ,{field:'auditnumber', title:'审核编号'}
                    ,{field:'audit_number', title:'申请单编号',sort: true}
                    ,{field:'audit_result', title:'审核结果'}
                    ,{field:'audit_info', title:'审核意见'}
                    ,{field:'audit_date', title:'审核时间'}
                    ,{fixed: 'right', title:'操作', toolbar: '#historyBarDemo'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
                ,text:{
                    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                }
            });
        }

        //详情表
        function detailInfo(number){
            table.render({
                type:1,
                elem: '#detailInfo'
                ,url:'${pageContext.request.contextPath}/rukudetail/showInfoBOUT?audit_number='+number
                ,title: '订单详情'
                ,cols: [[
                    // {type: 'checkbox', fixed: 'left'}
                    {field:'id', title:'ID',fixed: 'left',hide:true,unresize: true, sort: true}
                    ,{field:'audit_number', title:'采购编号'}
                    ,{field:'bookname', title:'书名'}
                    ,{field:'price', title:'进价'}
                    ,{field:'bookcount', title:'总量(本)',sort: true}
                    // ,{field:'count', title:'库存数量'}
                    ,{field:'publisher', title:'出版社'}
                ]]
                ,page: true
                ,limit:5 //设置一页几条
                ,limits:[5,10,15] //放置可供
                ,text:{
                    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                }
            });
        }


        //头工具栏事件
        table.on('toolbar(allApply)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });


        // 监听表单的头工具栏事件
        table.on('toolbar(outApply)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'outApply':
                    var outDatas = checkStatus.data;
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath}/insert',
                        data:JSON.stringify(outDatas),
                        contentType:'application/json',
                        dataType: 'json',
                        success:function (datasu) {
                            if(datasu.code==1){
                                layer.closeAll();
                                allApply();
                                layer.msg(datasu.msg);
                            }else {
                                layer.msg(datasu.msg);
                            }
                        }
                    });
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });


        //监听审核记录表的行工具事件  审核记录详情
        table.on('tool(auditHistory)', function(obj){
            var data=obj.data;
            if(obj.event==='allAuditInfo'){
                layer.open({
                    type: 1,
                    content: '<table class="layui-hide" id="detailInfo" lay-filter="detailInfo"></table>', //这里content是一个普通的String
                    success:function () {
                        detailInfo(data.audit_number);
                    }
                });
            }
        });

        //监听审核行工具事件
        table.on('tool(allApply)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'agree'){
                // alert(JSON.stringify(data));
                $.ajax({
                    type:'post',
                    url:'${pageContext.request.contextPath}/request/agree',
                    data:JSON.stringify(data),
                    contentType: 'application/json',
                    dataType:'json',
                    success:function (data) {
                        if (data.code==1){
                            allApply();
                            layer.msg(data.msg,{icon:1});
                        }else {
                            layer.msg(data.msg,{icon:5});
                        }
                    }
                })
            } else if(obj.event === 'disagree'){
                var disagreeinfo;
                layer.prompt({
                    formType: 2,
                    value: '原因.....',
                    title: '请输入不同意原因',
                    area: ['200px', '150px'] //自定义文本域宽高
                }, function(value, index, elem){
                    disagreeinfo=value; //得到value
                    layer.close(index);
                    alert(disagreeinfo);
                    // alert(JSON.stringify(data));
                    console.log(data);
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath}/audit/disagree',
                        data: {
                            "disagree":disagreeinfo,
                            "applyData":JSON.stringify(data)
                        },
                        //contentType:'application/json',
                        dataType:'json',
                        success:function (returnmsg) {
                            if (returnmsg.code==1){
                                allApply();
                                layer.msg(returnmsg.msg,{icon:1});
                            }else {
                                layer.msg(returnmsg.msg,{icon:5});
                            }
                        }
                    })

                });
            }else if(obj.event === 'allInfo'){
                //        查看详情
                //alert(data.number);
                layer.open({
                    type: 1,
                    content: '<table class="layui-hide" id="detailInfo" lay-filter="detailInfo"></table>', //这里content是一个普通的String
                    success:function () {
                        detailInfo(data.number)
                    }
                });
            }

        });


    });
</script>
</html>
